﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.Common.Domain;
using Traceability.Common.Dtos;
using Traceability.Common.Infrastructure.InterFaces;
using Traceability.Enum;

namespace Traceability.Bais.API.Appliction.Command.Users
{
    public class PermissionDeleteHandler : IRequestHandler<PermissionDeleteCommon, Result>
    {
        private readonly IBaseRepository<Permission> permissionRep;

        public PermissionDeleteHandler(IBaseRepository<Permission> _permissionRep)
        {
            permissionRep = _permissionRep;
        }
        public async Task<Result> Handle(PermissionDeleteCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.update };
            try
            {
                var permissions = await (await permissionRep.GetAll()).FirstOrDefaultAsync(c => c.PermissionId == request.PermissionId);
                
                if (permissions != null   )
                {
                    permissions.IsDelete = true;
                    var n = await permissionRep.UpdateAsync(permissions);
                    if(n>0)
                    {
                        result.Code = (int)CommonErrorCode.sucess;
                    }
                }


            }
            catch (Exception)
            {
                result.Msg = "修改失败";
                throw;
            }
            return result;
        }
    }
}
